import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/normal_entity.dart';
import 'package:wanxiang_app/pages/msg/chat.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../bean/detail_im_entity.dart';
import '../../bean/service_list_entity.dart';
import '../../utils/common.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';
import 'package:http/http.dart' as http;

class ServiceController extends GetxController{

  @override
  void onInit() {
    super.onInit();
    _getService();
  }
  @override
  void onClose() {
    super.onClose();
  }
   List<DetailImData> list = [];

  void _getService() async{

      Uri urls = Uri.https(Constant.baseUrlIm,ServiceUrl.getImByAccount,{'is_use':'2'});
      var response = await http.get(urls, headers: <String, String>{
        "appKey":Constant.appKey
      },
      );
      var parsedJson = json.decode(response.body);
      DetailImEntity blackEntity =DetailImEntity.fromJson(jsonDecode(parsedJson));
      // list = blackEntity.data;
      List<DetailImData> list1 = [];
      List<DetailImData> list2 = [];
      for(var item in blackEntity.data){
        if(item.isHidden!=1){
          if(item.isRest==0){
            list1.add(item);
          }else{
            list2.add(item);
          }
        }

      }
      list=[];
      list.addAll(list1);
      list.addAll(list2);
      update();
  }
}


class ServicePage extends GetView<ServiceController>  {
  late ServiceController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {

    Get.lazyPut<ServiceController>(() => ServiceController());
    controller = Get.find();
    context = context1;

    return GetBuilder<ServiceController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: '包赔客服',hasLeading: true,),
            backgroundColor: Colors.white,
            body:_content());
      },
    );

  }
  Widget _content(){
    return Container(
      margin: EdgeInsets.only(top: 10.h,bottom: 10.h),
      alignment: Alignment.center,
      child: _hotList()
    )  ;
  }


  Widget _hotList(){
    return
      Container(
          width: 328.w,
          height: MediaQuery.of(context).size.height,
          child: GridView.count(
            scrollDirection: Axis.vertical,
            childAspectRatio: 0.9,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
            crossAxisCount: 2,
            children: controller.list.map((item) => _item(item)).toList(),
          )
      );
  }

  Widget _item(DetailImData item){
    return  Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16.r),
        border: Border.all(color: Color(0xFFF4F4F4))
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              item.isRest==0?
              Container(
                width: 54.w,
                height: 21.h,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Color(0xFFFF9800),
                  borderRadius: BorderRadius.only(topLeft:Radius.circular(16.r),bottomRight:Radius.circular(16.r) ),
                ),
                child: Text('在线',style: TextStyle(
                    fontSize: 10.sp,
                    color: Colors.white
                ),),
              ):     Container(
                width: 54.w,
                height: 21.h,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Color(0xFFBBBBBB),
                  borderRadius: BorderRadius.only(topLeft:Radius.circular(16.r),bottomRight:Radius.circular(16.r) ),
                ),
                child: Text('休息',style: TextStyle(
                    fontSize: 10.sp,
                    color: Colors.white
                ),),
              )
              ,
            ],
          ),


          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text('${item.nickname}',style: TextStyle(
                color: Color(0xFF333333),
                fontSize: 13.sp
              ),),

              Image.network(Constant.IMG_NET_BASE+item.photo,width: 80.w,height: 80.h,),
              InkWell(
                onTap: (){
                  if( Common.checkLoginOrReal(context)){
                    Get.to(()=>ChatPage(),arguments: {
                      'sessionId':item.account,
                      'nickName':item.nickname,
                      'servicePhoto':item.photo,
                    },preventDuplicates:false);
                  }

                },
                child: item.isRest==0?

                Container(
                  alignment: Alignment.center,
                  width: 84.w,
                  height: 21.h,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(21.r),
                      color: Color(0xFF02D85F)
                  ),
                  child: Text('点击联系客服',style: TextStyle(
                      color: Colors.white,
                      fontSize: 10.sp
                  ),),
                ):
                Container(
                  alignment: Alignment.center,
                  width: 84.w,
                  height: 21.h,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(21.r),
                      color: Color(0xFFBBBBBB)
                  ),
                  child: Text('点击联系客服',style: TextStyle(
                      color: Colors.white,
                      fontSize: 10.sp
                  ),),
                )
              )


              
            ],
          )

        ],
      ),
    );
  }








}